package top.igotcha.chapter.chapter3;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @author Gotcha
 * @date 2021/3/8
 * @describe 可打断锁
 */
public class ReentrantLock01 {
    public static void main(String[] args) {
        ReentrantLock lock = new ReentrantLock();
        Thread t1 = new Thread(()-> {
            try {
                System.out.println("加可打断锁");
                //加锁，可打断锁
                lock.lockInterruptibly();
            } catch (InterruptedException e) {
                e.printStackTrace();
                //被打断，返回，不再向下执行
                return;
            }finally {
                System.out.println("释放锁");
                //释放锁
                lock.unlock();
            }
        });

        lock.lock();
        try {
            t1.start();
            Thread.sleep(1000);
            //打断
            t1.interrupt();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }
}
